using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Oops.Xna.Framework.Physics;
using Oops.Xna.Framework.Graphics;

namespace Oops.Xna.Framework.Testing.App
{
	public class Example8Screen : ExampleScreen
	{
		// Content.
		private Texture2D planeTexture;
		
		// Drawing helpers.
		private Quadrilateral quad;
		private Model botModel;
		private GpuAnimationPlayer animationPlayer;

		public Example8Screen(Game game)
			: base(game, "Example8") 
		{
			// Drawing helper for the ground.
			this.quad = new Quadrilateral(Vector3.Zero, Vector3.Up, Vector3.Backward, 260.0f, 260.0f, 10, 10);
		}

		#region ExampleScreen Members

		protected override void DrawWorld(Effect effect, EffectParameter world, EffectParameter texture)
		{
			// Draw the ground.
			world.SetValue(Matrix.Identity);
			texture.SetValue(this.planeTexture);
			effect.CommitChanges();
			this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, this.quad.Vertices, 0, 4, this.quad.Indices, 0, 2);

			foreach (ModelMesh mesh in this.botModel.Meshes)
			{
				foreach (Effect localEffect in mesh.Effects)
				{
					localEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(Vector3.Up * 5.0f));
					localEffect.Parameters["View"].SetValue(this.viewpoint.ActiveCamera.View);
					localEffect.Parameters["Projection"].SetValue(this.viewpoint.ActiveCamera.Projection);
					localEffect.Parameters["AnimationTexture"].SetValue((this.botModel.Tag as SkinningData).AnimationTexture);
					localEffect.Parameters["AnimationFrame"].SetValue(this.animationPlayer.AnimationFrame);
				}
				mesh.Draw();
			}
		}

		protected override void Reset()
		{
			// No physics.
			this.Reset(0, 0);
		}

		#endregion

		#region DrawableGameComponent Members

		protected override void LoadContent()
		{
			// Load all the used texture resources.
			this.planeTexture = this.Game.GetContent().Load<Texture2D>(@".\Content\Textures\plane");

			this.botModel = this.Game.GetContent().Load<Model>(@".\Content\Models\Bot");
			SkinningData data = this.botModel.Tag as SkinningData;
			this.animationPlayer = new GpuAnimationPlayer(data);
			this.animationPlayer.StartClip("Run");
			
			// Call inherited method.
			base.LoadContent();
		}

		#endregion

		#region IUpdateable Members

		public override void Update(GameTime gameTime)
		{
			this.animationPlayer.Update(gameTime);

			// Call inherited method.
			base.Update(gameTime);

			// Never shadowed.
			this.shadow = false;
		}

		#endregion
	}
}
